/**
 * Created by HUANGFR2 on 8/20/2016.
 */
const mongoose = require('mongoose');
var Complaint = mongoose.model("Complaint");
var Merchant = mongoose.model("Merchant");
var request = require('request');

exports.findAll = function(req, res){
    Complaint.find({}, function (err, complaints) {
        if(err){
            console.error('find all complaint error : ' + err);
            res.json({
                error : 'find all complaint error : ' + err
            });
        }else{
            var oids = [];
            complaints.forEach(function (complaint) {
                oids.push(complaint._id)
            });
            var formData = {
                oids : JSON.stringify(oids)
            };
            request.post({
                url: 'http://zha-ita113-w7:3038/orderWebService/getOrders',
                formData: formData
            }, function optionalCallback(err, httpResponse, body) {
                if (err) {
                    console.error('request order error : ' + err);
                    res.json({
                        error : 'request order error : ' + err
                    });
                    return;
                }
                try{
                    var complaints_ = JSON.parse(body);
                    complaints.forEach(function (complaint) {
                        complaint.detail = complaints_[complaint._id];
                    });
                    res.json({
                        msg : 'SUCCESS',
                        complaints : complaints
                    })
                } catch (err){
                    res.json({
                        error : 'request order error : Server Error'
                    });
                }
            });
        }
    })
};

exports.updateComplaintStatus = function (req, res) {
    var oid = req.body.oid;
    var status = req.body.status;
    Complaint.findOne({_id : oid}, function (err, complaint) {
        if(err){
            console.error('find complaint status error : ' + err);
            res.json({
                error : 'find complaint status error : ' + err
            });
        } else {
            complaint.status = status;
            complaint.save(function (err) {
                if(err){
                    console.error('update complaint error : ' + err);
                    res.json({
                        error : 'update complaint error : ' + err
                    });
                } else {
                    if(status == 3){
                        Merchant.update({_id : complaint.merchant}, { $set: { status: 3 }}, function (err) {
                            if(err){
                                console.error('update merchant status error : ' + err);
                            }
                        })
                    }
                    res.json({
                        msg : 'SUCCESS',
                        complaint : complaint
                    });
                }
            });
        }
    })
};

exports.findComplaintByMid = function (req, res) {
    var mid = req.params.mid;
    Complaint.find({merchant : mid}, {_id : 1, status : 1}, function (err, complaints) {
        if(err){
            console.error('find complaints by merchant error : ' + err);
            res.json({
                error : 'find complaints by merchant error : ' + err
            });
        } else {
            var complaints_ = {};
            complaints.forEach(function (complaint) {
                complaints_[complaint._id] = complaint.status;
            });
            res.json(complaints_);
        }
    });
};

exports.findComplaintByCid = function (req, res) {
    var cid = req.params.cid;
    Complaint.find({c_id : cid}, {_id : 1, status : 1}, function (err, complaints) {
        if(err){
            console.error('find complaints by client error : ' + err);
            res.json({
                error : 'find complaints by client error : ' + err
            });
        } else {
            var complaints_ = {};
            complaints.forEach(function (complaint) {
                complaints_[complaint._id] = complaint.status;
            });
            res.json(complaints_);
        }
    })
};
